﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = DateTime.Now.ToLongDateString().ToString() + DateTime.Now.DayOfWeek.ToString(); //年/月/日/星期
            Label2.Text = Application["VisitNumber"].ToString();
            int judge = 0;
            judge = Convert.ToInt32(Request["value"]);
            if (!IsPostBack)
            {
                if (judge == 1)
                    Response.Write("<script>alert('该用户已经登录！')</script>");
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Application.Lock();//锁定Application对象
            int num;     //在线人数
            string name; //登录用户
            string zs_name; 　//已在线的用户名
            string[] user;　//在线用户的数组
            num = int.Parse(Application["userNum"].ToString());
            if (nameTextBox.Text == "")
            {
                Response.Write("<script>alert('用户名不能为空')</script>");
                nameTextBox.Focus(); //获得TextBox1的焦点，也就是相当于鼠标自动点到那里
            }
            else
            {
                name = nameTextBox.Text.Trim();
                zs_name = Application["user"].ToString();
                user = zs_name.Split(',');
                for (int i = 0; i <= num - 1; i++)
                {
                    if (name == user[i].Trim())
                    {
                        int judge = 1;
                        Response.Redirect("Login.aspx?value=" + judge);
                    }
                }
                if (num == 0)
                    Application["user"] = name.ToString();
                else
                    Application["user"] = Application["user"] + "," + name.ToString();
                num += 1;
                Application["userNum"] = num;
                Session["userName"] = nameTextBox.Text.Trim();
                Application.UnLock();
                Response.Redirect("chat.aspx");
            }

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
              //Response.Write("<script>window.opener=null;window.close();</script>");// 不会弹出询问
            Response.Write("<script>window.close();</script>");// 会弹出询问是否关闭！只用于IE
        }
    }
}